package com.cn.ant.modules.wechat.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.modules.wechat.dao.NewsItemMapper;
import com.cn.ant.modules.wechat.entity.NewsItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
/**
 * 图文消息元素Service
 * @author AntDream
 * @version 2015-03-22
 */
@Component
@Transactional(readOnly = true)
public class NewsItemService extends BaseService {

	@Autowired
	private NewsItemMapper newsItemMapper;
	
	public NewsItem get(String id) {
		return newsItemMapper.get(id);
	}
	
	public Page<NewsItem> find(Page<NewsItem> page, Map<String,Object> param) {
        param.put("startIdx", page.getFirstResult());
        param.put("pageSize", page.getPageSize());
        page.setList(newsItemMapper.query(param));
        page.setCount(newsItemMapper.queryCount(param));
        return page;
	}

    /**
     * 根据图文消息模板,查找图文消息
     * @param templateId
     * @return
     */
    public List<NewsItem> findByTemplateId(String templateId) {
        return newsItemMapper.findByTemplateId(templateId);
    }

    @Transactional(readOnly = false)
	public void save(NewsItem newsItem) {
		newsItemMapper.insert(newsItem);
	}
	
	@Transactional(readOnly = false)
	public void update(NewsItem newsItem) {
		newsItemMapper.update(newsItem);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		newsItemMapper.delete(id);
	}
	
}
